package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Orders;
import org.csu.mypetstore.domain.UserCart;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.service.UserCartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ViewConfirmOrderServlet", value = "/ViewConfirmOrderServlet")
public class ViewConfirmOrderServlet extends HttpServlet {
    private static String CONFIRM_ORDER="/WEB-INF/jsp/order/ConfirmOrder.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        OrderService service=new OrderService();
        Orders orders=new Orders();
        orders.setItemid(request.getParameter("itemId"));
        System.out.println(orders.getItemid());
        UserCartService service1=new UserCartService();
        UserCart userCart=new UserCart();
        userCart=service1.getUserCart(orders.getItemid());
        userCart.setNumber(Integer.valueOf(request.getParameter("quantity")));
        //System.out.println(userCart.getListprice());
        //System.out.println(userCart.getNumber());
        userCart.setTotalprice(userCart.getListprice()*userCart.getNumber());
        userCart.setItemid(orders.getItemid());
        //System.out.println(userCart.getTotalprice());
        //System.out.println(request.getParameter("itemid"));
        session.setAttribute("USERCART",userCart);
        System.out.println(((UserCart)session.getAttribute("USERCART")).getTotalprice());
        request.getRequestDispatcher(CONFIRM_ORDER).forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
